package shuashuami.hb.com.http;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.share.ShareCUserInfo;

/**
 * Created by Administrator on 2017/7/10.
 */

public class UploadComplaintHttp {

    private Activity activity;

    private ProgressDialog progressDialog;

    public UploadComplaintHttp(Activity activity) {
        this.activity = activity;
    }

    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    private final OkHttpClient client = HttpUtils.getInstance();

    public void uploadImg(String id, String content, List<String> mImgUrls) {
        progressDialog = ProgressDialog.show(activity, "加载中...", "请稍后...", true, false);
        // mImgUrls为存放图片的url集合
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        for (int i = 0; i < mImgUrls.size() - 1; i++) {
            File f = new File(mImgUrls.get(i));
            if (f != null) {
                builder.addFormDataPart("images[]", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
            }
        }
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(activity);
        String brushId = shareCUserInfo.getId();
        //添加其它信息
        builder.addFormDataPart("brushId", brushId);
        builder.addFormDataPart("shop_id", id);
        builder.addFormDataPart("content", content);
        MultipartBody requestBody = builder.build();
        //构建请求
        Request request = new Request.Builder()
                .url(CURLs.complaintUrl)//地址
                .post(requestBody)//添加请求体
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = handler.obtainMessage(2);     // Message
                handler.sendMessageDelayed(message, 0);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                Message message = handler.obtainMessage(1);     // Message
                message.obj = response.body().string();
                handler.sendMessageDelayed(message, 0);
            }
        });

    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        progressDialog.dismiss();
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(activity);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            activity.setResult(Activity.RESULT_CANCELED);
                            activity.finish();
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                        } else {
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    progressDialog.dismiss();
                    ToastUtil.showShort(activity, "网络访问错误");
                    break;

            }
            super.handleMessage(msg);
        }
    };

}
